import { Request, Response } from 'express';
import evaluationService from '../../services/evaluation.service'
import orderService from '../../services/order.service';
export class Controller {
    // 创建评价
    create(req: Request, res: Response): void {
        // 解析请求参数
        let uid = (req.headers as any).user.id
        let body = req.body as any[]
        // 创建评价接口支持一次请求创建一组评价，进行评价组的遍历
        body.forEach((e:any)=>{
            evaluationService.createEvaluation({ 
                user_id: uid,
                gid: e.gid,
                content: e.content ?? "",
                star: e.star ?? 0,
                state: e.state ?? 0
            }).catch((error)=>{
                res.status(200).json({ msg: 'error', error: error })
            })
        })
        let oid = Number(req.query.oid as string)
        // 更新订单状态
        orderService.update(oid, 3).then()
        res.status(200).json({ msg: 'ok' })
    }

    // 更新评价状态
    update(req: Request, res: Response): void {
        let state = Number(req.query.state as string)
        let id =  Number(req.query.eid as string)
        if (!id) {
            res.status(200).json({ msg:'error', error:'请指定要更新的类别id' })
        }
        evaluationService.updateEvaluation({id, state}).then(()=>{
            res.status(200).json({ msg:'ok' })
        })
    }

    // 获取评价列表
    listGet(req: Request, res: Response): void {
        let gid = undefined
        if (req.query.gid) {
            gid = Number(req.query.gid as string)
        }
        let filter = Number(req.query.filter as string) ?? 0
        let offset = Number(req.query.offset as string)
        let limit = Number(req.query.limit as string)
        evaluationService.getList(gid, filter, offset, limit).then((data)=>{
            res.status(200).json({ msg: 'ok',datas: data })
        }).catch((error)=>{
            res.status(200).json({ msg: 'error', error: error })
        })
    }
}
export default new Controller();